package logic

import (
	"context"
	"errors"
	"github.com/eeppee_admin/clouddisk/service/internal/svc"
	"github.com/eeppee_admin/clouddisk/service/internal/types"
	"github.com/eeppee_admin/clouddisk/service/model"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserDetailLogic {
	return &UserDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// http GET :8888/user/detail identity=dcf31a17-a7b8-4cf5-bcd2-ecad9996499d
func (l *UserDetailLogic) UserDetail(req *types.DetailRequest) (resp *types.DetailResponse, err error) {
	resp = &types.DetailResponse{}

	user := new(model.User)

	has, err := l.svcCtx.Engine.Where("identity = ?", req.Identity).Get(user)

	if err != nil {
		return nil, err
	}

	if !has {
		return nil, errors.New("用户不存在")
	}

	resp.Name = user.Name
	resp.Email = user.Email

	return
}
